package com.hk.service.business.chrome.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.domain.entity.ReportPdf;
import com.hk.enums.YxbzEnum;
import com.hk.mapper.ReportPdfMapper;
import com.hk.service.business.chrome.ReportPdfService;
import com.hk.utils.ConstantUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

/**
 * <p>
 * pdf报告表 服务实现类
 * </p>
 *
 * @author dpb
 * @since 2023-04-16
 */
@Service
public class ReportPdfServiceImpl extends ServiceImpl<ReportPdfMapper, ReportPdf> implements ReportPdfService {


    @Override
    public void uploadFile(String pdfFile) throws Exception {
        ReportPdf reportPdf = new ReportPdf();
        reportPdf.setId(ConstantUtil.getUUID());
        reportPdf.setRiskTaskId(3424235L);
        reportPdf.setWjns(buildBase64(pdfFile));
        reportPdf.setYxbz(YxbzEnum.Y.name());

        save(reportPdf);
    }

    private String buildBase64(String pdfFile) throws Exception {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pdfFile));
        int len = bis.available();
        byte[] b = new byte[len];
        bis.read(b, 0, len);
        org.apache.commons.net.util.Base64 base64 = new org.apache.commons.net.util.Base64();
        String base63Str = base64.encodeToString(b);
        return StringUtils.isNotBlank(base63Str) ? base63Str : "";

    }
}
